(require 'workspace-model)

(defvar *class-reg* "class[\s\t\r\n]+[a-zA-Z0-9_]+")
(defvar *function-reg* "\\(\\(public\\)\\|\\(private\\)\\|\\)[\s\t\r\n]*function[^
{]+:[^{]+")
(defvar *no-space-reg* "\\([A-Za-z0-9_]+\\)\\|\\([;():,]\\)")
(defvar *variable-reg* "\\(\\(public\\)\\|\\(private\\)\\|\\)[\s\t\r\n]*var[^;]*;")

(defun read-file (filename)
  "add files infomation to memory."
  (let* ((file-buffer "")
	 (classinfo nil))
    (with-temp-buffer
      (insert-file-contents filename 't)
      (setq file-buffer (buffer-string))
      (setq classinfo (parse-file file-buffer))
      (oset classinfo path filename)
      (add-class  classinfo))))

(defun refresh-current-file ()
  "parse current file"
  (refresh-current-class (parse-file (buffer-string))))

(defun parse-file (file-buffer)
  "parse buffer and return a class instance"
  (let* ((word-buffer "")
	 (function-list '())
	 (variable-list '())
	 (class-info (make-instance class))
	 (file-region-buffer nil)
	 (sentence-region-buffer nil)
	 (buffer nil))
    (string-match *class-reg* file-buffer)
    (setq buffer (match-string 0 file-buffer))
    (string-match "[^\s]*$" buffer)
    (setq buffer (match-string 0 buffer))
    ;;befor add to list 
    ;;claess's name must be specified
    (oset class-info :name buffer)
    ;;;search variables
    (let* ((variable-start 0)
	   (variable-end nil)
	   (variable-string ""))
      (while (setq variable-start
		    (string-match *variable-reg* file-buffer variable-start))
	(setq variable-end (match-end 0))
	(setq variable-string (match-string 0 file-buffer))
;	(debug)
	;;;composite variable
	(let* ((word "")
	       (word-start 0)
	       (word-end nil)
	       (state 'var-name)
	       (variable-instance (make-instance variable :scope "private")))
	  (while (setq word-start 
			(string-match *no-space-reg* variable-string word-start))
;	    (debug)
	    (setq word-end (match-end 0))
	    (setq word (match-string 0 variable-string))
	    ;;;deal a single word
	    (cond ((equal word "public")
		   (oset variable-instance scope "public"))
		  ((equal word "private")
		   (oset variable-instance scope "private"))
		  ((equal word "get"))
		  ((equal word "set"))
		  ((equal word "var")
		   (setq state 'var-name))
		  ((equal word ";")
		   (setq state 'end))
		  ((equal word ":")
		   (setq state 'var-type))
		  ((equal word "=")
		   (setq state 'end))
		  ('t
		   (cond ((equal state 'var-name)
			  (oset variable-instance name word))
			 ((equal state 'var-type)
			  (oset variable-instance type word)))))
	  ;;;finish dealing a single 
	    (setq word-start word-end))
	  (add-variable variable-instance class-info)
	  (setq variable-instance (make-instance variable :scope "private")))
	;;;finish composite funtion

	(setq variable-start variable-end)))
    ;;;search functions
    (let* ((function-start 0)
	   (function-end nil)
	   (function-string ""))
      (while (setq function-start
		    (string-match *function-reg* file-buffer function-start))
	(setq function-end (match-end 0))
	(setq function-string (match-string 0 file-buffer))
	;;;composite function
	(let* ((word "")
	       (word-start 0)
	       (word-end nil)
	       (state 'fun-name)
	       (function-instance (make-instance function :scope "public"))
	       (parameter-instance (make-instance parameter)))
	  (while (setq word-start 
			(string-match *no-space-reg* function-string word-start))
;	    (debug)
	    (setq word-end (match-end 0))
	    (setq word (match-string 0 function-string))
	    ;;;deal a single word
	    (cond ((equal word "public")
		   (oset function-instance scope "public"))
		  ((equal word "private")
		   (oset function-instance scope "private"))
		  ((equal word "function"))
		  ((equal word ";")
		   (setq state 'end))
		  ((equal word ":")
		   (if (equal state 'var-name)
		       (setq state 'var-type)
		     (setq state 'fun-type)))
		  ((equal word "(")
		   (setq state 'var-name))
		  ((equal word ")")
		   (add-parameter parameter-instance function-instance)
		   (setq parameter-instance (make-instance parameter))
		   (setq state 'fun-type))
		  ((equal word ",")
		   (add-parameter parameter-instance function-instance)
		   (setq parameter-instance (make-instance parameter))
		   (setq state 'var-name))
		  ('t
		   (cond ((equal state 'fun-name)
			  (oset function-instance name word))
			 ((equal state 'var-name)
			  (oset parameter-instance name word))
			 ((equal state 'var-type)
			  (oset parameter-instance type word))
			 ((equal state 'fun-type)
			  (oset function-instance return-type word)))))
	  ;;;finish dealing a single 
	    (setq word-start word-end))
	  (add-function function-instance class-info)
	  (setq function-instance (make-instance function :scope "public")))
	;;;finish composite funtion
	(setq function-start function-end)))
    class-info))

(provide 'as-load)